//
//  LGHomeController.m
//  MindBox
//
//  Created by MS on 15-12-31.
//  Copyright (c) 2015年 Lemon. All rights reserved.
//

#import "LGHomeController.h"
#import "GoodsDetailController.h"

#import "AdsDetailController.h"
#import "PopCatPageController.h"
#import "GiftPageController.h"
#import "PopCatPageModel.h"
#import "WeekPageController.h"
#import "GoodsController.h"
#import "GoodsListController.h"
#import "SaoMiaoController.h"

#import "LGSeachBar.h"
#import "HomePageTool.h"
#import "HomeModel.h"
#import "HomeGoodsModel.h"
#import "HomePageView.h"

@interface LGHomeController ()<UISearchBarDelegate,HomePageDelegate,UITextFieldDelegate>
@property (nonatomic,weak) LGSeachBar * searchBar;
@property (nonatomic,weak) HomePageView * homePageView ;
@property (nonatomic,strong) NSMutableArray * goodsData;
@property (nonatomic,strong) Data * data;
@property (nonatomic,weak)  MJRefreshAutoFooter * footerFresh;
@property (nonatomic,weak)  MJRefreshNormalHeader * headerRefresh;
@property (nonatomic,assign) int pages;
@end

@implementation LGHomeController

-(HomePageView *)homePageView
{
    if (_homePageView==nil) {
        HomePageView * homePageView = [[HomePageView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-49)];
        homePageView.delegate =self;
        _homePageView = homePageView;
        [self.view addSubview:_homePageView];
    }
    return _homePageView;
}

-(NSMutableArray *)goodsData
{
    if (_goodsData==nil) {
        _goodsData = [[NSMutableArray alloc] init];
    }
    return _goodsData;
}
-(Data *)data
{
    if (_data==nil) {
        _data = [[Data alloc] init];
    }
    return _data;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    
    [self setUpNavBar];
    
    [self loadHomeGoodsData];
    [self loadHomePageData];
    
    [self addHeaderRefresh];
    [self addFootRefresh];
    
}

-(void)loadHomePageData
{
    [HomePageTool homePageDataWithSuccess:^(Data *data) {
        
        [_footerFresh endRefreshing];
        [_headerRefresh endRefreshing];
        self.data = data;
        self.homePageView.data = self.data;
       

    } failure:^(NSError *error) {
//        NSLog(@"homePageData:%@",error);
    }];
 
}

-(void)loadHomeGoodsData
{
    
    [HomePageTool homePageGoodsDataWithPages:++_pages Success:^(NSArray *dataArray) {
        [_footerFresh endRefreshing];
        [_headerRefresh endRefreshing];

        if (dataArray.count) {
            [self.goodsData addObjectsFromArray:dataArray];
        }
        
        self.homePageView.goodsData = self.goodsData;
        
    } failure:^(NSError *error) {
//        NSLog(@"homePageGoodsData:%@",error);
    }];

}

-(void)addFootRefresh
{
    __weak typeof(self) weakSelf = self;
    MJRefreshAutoNormalFooter * footerFresh = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadHomeGoodsData];
    }];
    
    self.footerFresh = footerFresh;
    self.homePageView.collectionView.mj_footer = footerFresh;
    
}

-(void)addHeaderRefresh
{
    __weak typeof(self) weakSelf = self;

    MJRefreshNormalHeader * headerRefresh = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pages = 0;
        [self.goodsData removeAllObjects];
//        [weakSelf loadHomePageData];
        [weakSelf loadHomeGoodsData];
    }];
    
    self.headerRefresh = headerRefresh;
    self.homePageView.collectionView.mj_header = headerRefresh;
    
}

-(void)setUpNavBar
{    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"pohto2"] andHighImage:[UIImage imageNamed:@"pohto2"] target:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside];
    
      self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"right1"] andHighImage:[UIImage imageNamed:@"right1"] target:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];
    
        LGSeachBar * searchBar = [[LGSeachBar alloc] initWithFrame:CGRectMake(0, 0, 220, 22)];
        searchBar.layer.cornerRadius = 9;
            searchBar.clipsToBounds= YES;
        searchBar.image = [UIImage imageNamed:@"ic_search"];
        searchBar.placeHolder = @"搜索喜欢的礼物";
        searchBar.font = [UIFont systemFontOfSize:12];
        searchBar.delegate = self;
    
        self.searchBar = searchBar;
        self.navigationItem.titleView = searchBar;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchBarIuput:) name:UITextFieldTextDidChangeNotification object:nil];
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.searchBar endEditing:YES];
    
    GoodsListController * vc  = [[GoodsListController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.name_id = textField.text;
    vc.titleName = textField.text;
    [self.navigationController pushViewController:vc animated:YES];
    
    return YES;
}

-(void)searchBarIuput:(NSNotification *)note
{
    if (self.searchBar.text.length) {
        
        self.searchBar.placeHolderhidden = YES;
        
    }else
    {
        self.searchBar.placeHolderhidden = NO;
    }
    
}

#pragma mark homePageView代理方法
-(void)homePageBannerDidClick:(HomePageView *)homePage banner:(Banner *)banner index:(NSInteger)index
{
    AdsDetailController * vc = [[AdsDetailController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.urlStr = banner.name;
    vc.title = banner.url_name;
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(void)homePageBonus_BannerDidClick:(HomePageView *)homePage bonus_banner:(Bonus_Banner *)bonus_banner index:(NSInteger)index
{
//    NSLog(@"bonus_banner:--%ld--%@",index,bonus_banner.title);

}

-(void)homePageGift_TopicsViewDidClick:(HomePageView *)homePage gift_Topics:(Gift_Topics *)gift_Topic index:(NSInteger)index
{
    GiftPageController * vc = [[GiftPageController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.cat_id = gift_Topic.go_id;
    [self.navigationController pushViewController:vc animated:YES];
}

/**
 *  PmtView
 *  @param pmt      有值则点击了商品，没有点击了“全部”
 *  @param index    为“-1”是点击了“全部”
 */
//闪送抢购
-(void)homePmtViewDidClick:(HomePageView *)homePage pmt:(Pmt *)pmt index:(NSInteger)index
{
    if (index!=-1) {
        GoodsController * vc  = [[GoodsController alloc] init];
        vc.good_id = pmt.goods_id;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else{
        NSLog(@"跳转全部");
    }
}

/**
 *  PmtView
 *  @param week      有值则点击了商品，没有点击了“全部”
 *  @param index    为“-1”是点击了“全部”
 */
-(void)homeWeekViewDidClick:(HomePageView *)homePage week:(Week *)week index:(NSInteger)index
{
    WeekPageController * vc = [[WeekPageController alloc] init];
    
    vc.hidesBottomBarWhenPushed = YES;
    vc.index = index;
    [self.navigationController pushViewController:vc animated:YES];
//    NSLog(@"week:%ld--%@",index,week.short_name);
}

-(void)homePageTail_BannerDidClick:(HomePageView *)homePage tail_banner:(Tail_Banner *)tail_banner index:(NSInteger)index
{
    if ([tail_banner.table_name isEqualToString:@"welfare"]) {
        
        for (UIView * view in self.tabBarController.tabBar.subviews){
            if ([view isKindOfClass:[MyTabbar class]]) {
                MyTabbar * tabbar = (MyTabbar *)view;
                [tabbar selectIndex:2];
            }
            
        }
    }
    else{
        AdsDetailController * vc = [[AdsDetailController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.urlStr = tail_banner.name;
        vc.title = tail_banner.url_name;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(void)homePagePop_CatDidClick:(HomePageView *)homePage pop_cat:(Pop_Cat *)pop_cat index:(NSInteger)index
{
    PopCatPageController * vc = [[PopCatPageController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.cat_id = pop_cat.go_id;
    vc.title = pop_cat.obj_name;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)homeGoodsDidClick:(HomePageView *)homePage goods:(GoodsData *)goods index:(NSInteger)index
{
    GoodsController * vc  = [[GoodsController alloc] init];
    vc.good_id = goods.goods_id;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)homePageDidScroll:(HomePageView *)homePage
{
    [self.searchBar endEditing:YES];

}

-(void)rightClick
{
    
//    NSLog(@"点击了右边");
}
-(void)leftClick
{
    SaoMiaoController *controller = [[SaoMiaoController alloc] init];
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
//    NSLog(@"点击了左边");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
